﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using Common;
using ManagerMachine.Converter;


namespace ManagerMachine
{
    public partial class ServerMachineGroup
    {
        public ManagerProcess manager_process;
        ManagerTable manager_table;
        LoginServerConnection connectionLogin;
        public CONFIG config;
        string path_config = Directory.GetCurrentDirectory() + "\\Config\\CONFIG.xml";
        ConvertDataLogin convert_data_login;
        public ServerMachineGroup()
        {
            LoadConfig();           
            Init();
        }
        public void Init()
        {
            manager_process = new ManagerProcess(config);
            manager_table = new ManagerTable(config);
            convert_data_login = new ConvertDataLogin();
            manager_table.NewTableConnected += new EventHandler<TableConnectedEventArgs>(manager_table_NewTableConnected);
            connectionLogin = new LoginServerConnection();
            connectionLogin.Receive_From_Login_Server += new EventHandler<ReceiveFromLoginEventArgs>(connectionLogin_Receive_From_Login_Server);
        }

               

        void LoadConfig()
        {
            config = CONFIG.Load(path_config);
        }

        public void StartLoginServer()
        {            
            //manager_table.Start();
            connectionLogin.Connect(config.IP_LOGIN_SERVER, config.PORT_LOGIN_SERVER);
            InitRooms();
        }

        private void InitRooms()
        {
            //throw new NotImplementedException();
        }

        void connectionLogin_Receive_From_Login_Server(object sender, ReceiveFromLoginEventArgs e)
        {
            MSG_GSM message = (MSG_GSM)e.Data[0];
            Console.WriteLine("Receive: " + message.ToString());
            List<object> param = new List<object>();
            for (int i = 1; i < e.Data.Length; i++)
            {
                param.Add((byte)e.Data[i]);
            }
            try
            {
                Type t = typeof(ServerMachineGroup);
                MethodInfo handler = t.GetMethod("Login_Server_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                handler.Invoke(this, new object[] { param.ToArray() });
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error MSG_GSM." + message.ToString());
            }        

        }

        void manager_table_NewTableConnected(object sender, TableConnectedEventArgs e)
        {
            e.table.ReceiveFromTable += new EventHandler<ReceiveFromTableEventArgs>(table_ReceiveFromTable);
        }

        void table_ReceiveFromTable(object sender, ReceiveFromTableEventArgs e)
        {
            MSG_GSM message = (MSG_GSM)e.Data[0];
            Console.WriteLine("Receive: " + message.ToString());
            List<object> param = new List<object>();
            for (int i = 1; i < e.Data.Length; i++)
            {
                param.Add((byte)e.Data[i]);
            }
            try
            {
                Type t = typeof(ServerMachineGroup);
                MethodInfo handler = t.GetMethod("Table_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                handler.Invoke(this, new object[] { param.ToArray() });
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error MSG_GSM." + message.ToString());
            }     
        }
    }
}
